﻿using COFE.Core;
using COFE.Core.Entry;
using COFE.Core.EntryType;
using QuickZip.Converters;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace COFE.Core.Lister
{
    public class ShellIconPropertyProvider : ISecondaryPropertyProvider<IFileSystemInfo>
    {
        #region Constants

        public static string SupportedExtensions = ".exe,.ico";

        #endregion

        #region Methods

        public object[] GetSupportedProperty(IFileSystemInfo sourceObj)
        {
            object[] iconProperties = new object[] {
                IconProperties.Icon,
                IconProperties.SmallIcon, 
                IconProperties.LargeIcon, 
                IconProperties.ExtraLargeIcon,
                IconProperties.JumboIcon
            };


            if (sourceObj is IFileInfo && sourceObj.IsFileSystem)
            {
                var supExt = SupportedExtensions.Split(',');

                if (supExt.Contains(COFEPath.GetExtension(sourceObj.ParseName).ToLower()))
                    return iconProperties;
            }

            return new object[] { };
        }

        public bool IsPropertyReadOnly(IFileSystemInfo sourceObj, object property)
        {
            return true;
        }

        public PropertyPair GetProperty(IFileSystemInfo sourceObj, object property)
        {
            Func<IconProperties, IconSize> convertSize = (s) =>
                {
                    switch (s)
                    {
                        case IconProperties.ExtraLargeIcon: return IconSize.extraLarge;
                        case IconProperties.JumboIcon: return IconSize.jumbo;
                        case IconProperties.LargeIcon: return IconSize.large;
                        case IconProperties.SmallIcon: return IconSize.small;
                    }
                    return IconSize.small;
                };


            if (property == null || !(property is IconProperties))
                throw new ArgumentException();

            if (sourceObj is IFileInfo && sourceObj.IsFileSystem)
            {
                if ((int)property == (int)IconProperties.Thumbnail)
                    throw new NotImplementedException();
                //return new PropertyPair(property,
                //        ResourceListAttributeExtension.GetResourceList((IPropertyHost)sourceObj, property));          

                IconSize size = convertSize((IconProperties)property);

                Bitmap retVal = null;

                if (size == IconSize.large)
                    retVal = Icon.ExtractAssociatedIcon(sourceObj.FullName).ToBitmap();
                else
                    using (var imgList = new SystemImageList(size))
                    {
                        retVal = imgList[sourceObj.FullName, false];
                    }

                retVal.MakeTransparent();
                retVal = retVal.EnsureBitmapSize((IconProperties)property);
                return new PropertyPair(property, retVal);
            }

            throw new NotSupportedException();
        }

        public void SetProperty(IFileSystemInfo sourceObj, object property, object value)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
